package com.mianyang.zhangsan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mianyang.zhangsan.common.util.CommonUtils;
import com.mianyang.zhangsan.common.util.RequestHelper;
import com.mianyang.zhangsan.mapper.SecVideoTaskMapper;
import com.mianyang.zhangsan.model.entity.SecVideo;
import com.mianyang.zhangsan.model.entity.SecVideoTask;
import com.mianyang.zhangsan.service.SecVideoService;
import com.mianyang.zhangsan.service.SecVideoTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author jiangjr
 */
@Service
public class SecVideoTaskServiceImpl extends ServiceImpl<SecVideoTaskMapper, SecVideoTask> implements SecVideoTaskService {
    @Autowired
    private SecVideoService videoService;

    @Override
    public Page<SecVideoTask> selectTaskByAccountId(Long accountId, int current, int pageSize) {
        QueryWrapper<SecVideoTask> wrapper = new QueryWrapper<>();
        if (accountId != null) {
            wrapper.eq("account_id", accountId);
            return page(new Page<>(current, pageSize), wrapper);
        }
        return null;
    }

    @Override
    public List<SecVideo> selectTaskVideos() {
        QueryWrapper<SecVideoTask> wrapper = new QueryWrapper<>();
        Long accountId = RequestHelper.currentAccountId();
        if (accountId != null) {
            wrapper.eq("account_id", accountId);
            List<SecVideoTask> tasks = list(wrapper);
            if (CommonUtils.isNotEmpty(tasks)) {
                List<Long> videoIds = tasks.stream().map(SecVideoTask::getVideoId).collect(Collectors.toList());
                return videoService.listByIds(videoIds);
            }
        }
        return null;
    }
}
